package org.example.message_queue.demo;

import org.example.message_queue.common.Consumer;
import org.example.message_queue.common.MqException;
import org.example.message_queue.mqclient.Channel;
import org.example.message_queue.mqclient.Connection;
import org.example.message_queue.mqclient.ConnectionFactory;
import org.example.message_queue.mqserver.core.BasicProperties;
import org.example.message_queue.mqserver.core.ExchangeType;

import java.io.IOException;

public class DemoConsumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("启动消费者！");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(9090);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("testQueue",true,false,false,null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                System.out.println("[消费数据]开始！");
                System.out.println("consumerTag: " + consumerTag);
                System.out.println("basicProperties: " + basicProperties);
                String bodyStr = new String(body,0,body.length);
                System.out.println("bodyStr: " + bodyStr);
                System.out.println("[消费数据]结束!");
            }
        });

//        一直等待消费
        while(true){
            Thread.sleep(500);
        }
    }
}
